package interfaces.tables;

import fileHandling.language.Lang;
import interfaces.GUISource;
import interfaces.language.LanguageLabel;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.StaticContent;

import java.util.ArrayList;
import java.util.List;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

import com.jme.system.DisplaySystem;

public class ChoiceContainer<T extends TableButton> extends StaticContent {
	
	private ButtonTable<T> choiceTable, selectionTable;
	private T selectedAddButton, selectedRemoveButton;
	private InterfaceButton addButton, removeButton;
	private LanguageLabel choicesLabel, selectionLabel;
	private int maxSelect, lastRemovedIndex = -1;
	private boolean addWasPossible = true, removeWasPossible;
	private IButtonPressedListener addListener;
	
	public ChoiceContainer(int width, int height, int x, int y, Lang choices, Lang selection, int maxSelect) {
		super(width, height, x, y);
		if(maxSelect <= 0) this.maxSelect = Integer.MAX_VALUE;
		else this.maxSelect = maxSelect;
		
		ImageFont labelFont = GUISource.middleFont;
		int labelHeight = labelFont.getHeight();
		
		choicesLabel = new LanguageLabel(choices, labelFont);
		choicesLabel.setSize(width / 2, labelHeight);
		choicesLabel.setXY(0, height - choicesLabel.getHeight());
		addWidget(choicesLabel);
		
		choiceTable = new ButtonTable<T>(width / 3, height - choicesLabel.getHeight());
		int tableY = 0;
		choiceTable.setXY(0, tableY);
		addWidget(choiceTable);
		
		selectionLabel = new LanguageLabel(selection, labelFont);
		selectionLabel.setHeight(labelHeight);
		
		selectionTable = new ButtonTable<T>(width / 3, height - selectionLabel.getHeight());
		selectionTable.setXY(width / 3 * 2, tableY);
		addWidget(selectionTable);
		
		int selectionX = selectionTable.getX();
		if(width - selectionX < selectionLabel.getWidth()) selectionX = width - selectionLabel.getWidth();
		selectionLabel.setXY(selectionX, height - selectionLabel.getHeight());
		addWidget(selectionLabel);
		
		setButtons();
	}

	private void setButtons() {
		int buttonSize = DisplaySystem.getDisplaySystem().getHeight() / 22;
		int buttonX = (width / 2) - (buttonSize / 2);
		int buttonY = (height / 2) - (buttonSize / 2);
		
		addButton = new InterfaceButton(">", GUISource.middleFont);
		addButton.setSize(buttonSize, buttonSize);
		addButton.setXY(buttonX, buttonY);
		addListener = new IButtonPressedListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(selectedAddButton != null && selectionTable.getButtonCount() < maxSelect) {
					addWasPossible = true;
					TableButton button = selectedAddButton.getCopy();
					GUISource.setFont(button, GUISource.bigFont);
					addSelectedButton((T)button);
					removeWasPossible = true;
				} else addWasPossible = false;
			}
		};
		addButton.addButtonPressedListener(addListener);
		
		removeButton = new InterfaceButton("<", GUISource.middleFont);
		removeButton.setSize(buttonSize, buttonSize);
		removeButton.setXY(buttonX, buttonY);
		removeButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(selectionTable.getButtonCount() != 0 && selectedRemoveButton != null) {
					lastRemovedIndex = selectionTable.getButtonIndex(selectedRemoveButton);
					selectionTable.removeButton(selectedRemoveButton);
					removeWasPossible = true;
					addWasPossible = true;
				} else removeWasPossible = false;
			}
		});
	}
	
	public ArrayList<String> getSelection() { return selectionTable.getSelection(); }
	
	public void addSelectedButton(T button) {
		button.addButtonPressedListener(new IButtonPressedListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				selectedRemoveButton = (T)e.getTrigger();
				addWidget(removeButton);
				removeWidget(addButton);
				layout();
			}
		});
		selectionTable.addButton(button);
	}
	
	public void addChoiceButton(T button) {
		button.addButtonPressedListener(new IButtonPressedListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				selectedAddButton = (T)e.getTrigger();
				addWidget(addButton);
				removeWidget(removeButton);
				layout();
			}
		});
		choiceTable.addButton(button);
	}
	
	public boolean addWasPossible() { return addWasPossible; }
	
	public boolean removeWasPossible() { return removeWasPossible; }
	
	public void addListenerToAddButton(IButtonPressedListener listener) {
		addButton.addButtonPressedListener(listener);
	}
	
	public void addListenerToRemoveButton(IButtonPressedListener listener) {
		removeButton.addButtonPressedListener(listener);
	}
	
	public T getSelectedAddButton() { return selectedAddButton; }
	
	public T getSelectedRemoveButton() { return selectedRemoveButton; }
	
	public void setSelectedRemoveButton(T b) { selectedRemoveButton = b; }
	
	public InterfaceButton getAddButton() { return addButton; }
	
	public InterfaceButton getRemoveButton() { return removeButton; }
	
	public IButtonPressedListener getAddListener() { return addListener; }
	
	public int getLastRemovedIndex() { return lastRemovedIndex; }
	
	public int getButtonIndex(T button) { return selectionTable.getButtonIndex(button); }
	
	public boolean isSelectionButton(T button) { return selectionTable.hasButton(button); }
	
	public void clearSelection() {
		selectionTable.clear();
		removeWidget(addButton);
		removeWidget(removeButton);
	}
	
	public void setMaxSelect(int maxSelect) { this.maxSelect = maxSelect; }
	
	public List<T> getChoiceButtons() { return choiceTable.getButtons(); }
	
	public void changeButtonTexts(String addText, String removeText) {
		addButton.setText(addText);
		updateButtonPos(addButton);
		
		removeButton.setText(removeText);
		updateButtonPos(removeButton);
	}
	
	private void updateButtonPos(InterfaceButton button) {
		int buttonWidth = button.getWidth();
		int x = (getWidth() / 2) - (buttonWidth / 2);
		button.setX(x);
	}
	
	public void removeCenterButtons() {
		removeWidget(removeButton);
		removeWidget(addButton);
		layout();
	}
}